package com.goods.business.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.model.business.Supplier;

import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: Mr.Cao
 * @DateTime: 2021/11/9 15:16
 * BMW F750GS
 **/
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;


    /**
     * 分页获取物资来源
     * */
    @Override
    public PageVO findSupplierList(Map queryMap) {
        //获取查询条件
        String name = (String) queryMap.get("name");
        String contact = (String) queryMap.get("contact");
        String address = (String) queryMap.get("address");
        //条件查询
        Example o = new Example(Supplier.class);
        Example.Criteria criteria = o.createCriteria();
        if (!StringUtils.isEmpty(address)){
            criteria.andLike("address","%"+address+"%");
        }
        if (!StringUtils.isEmpty(contact)){
            criteria.andEqualTo("contact",contact);
        }
        if (!StringUtils.isEmpty(name)){
            criteria.andLike("name","%"+name+"%");
        }
        String pageNum = (String) queryMap.get("pageNum");
        String  pageSize = (String) queryMap.get("pageSize");
        //通过PageHelper实现分页：每页显示5条记录
        Page<Object> page = PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        List<Supplier> supplierList =   supplierMapper.selectByExample(o);
        //转Vo
        List<SupplierVO> supplierVOList = supplierList.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
        //排序
        Collections.sort(supplierVOList, (o1, o2) -> o1.getSort()-o2.getSort());
        return new PageVO(page.getTotal(),supplierVOList);
    }
    /**
     * 添加物资来源
     * */
    @Override
    public void add(SupplierVO supplierVO) {
        if (supplierVO!=null){
            Supplier supplier = new Supplier();
            BeanUtils.copyProperties(supplierVO,supplier);
            supplier.setCreateTime(new Date());
            supplier.setModifiedTime(new Date());
            supplierMapper.insert(supplier);
        }
    }


    /**
     * 删除物资来源
     * */
    @Override
    public void delete(long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

    /**
     * 编辑物资来源时,数据的回显
     * */
    @Override
    public SupplierVO edit(long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        if (null!=supplier){
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier,supplierVO);
            return supplierVO;
        }
        return null;
    }
    /**
     * 更新物资来源
     * */
    @Override
    public void update(long id, SupplierVO supplierVO) {
        //先查询
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        if (supplier!=null){
            //复制属性
            BeanUtils.copyProperties(supplierVO,supplier);
            //设置更新时间
            supplier.setModifiedTime(new Date());
            //更新数据
            supplierMapper.updateByPrimaryKey(supplier);
        }
    }
    /**
     * 查询全部物资
     * */
    @Override
    public List findAll() {
        List<Supplier> supplierList = supplierMapper.selectAll();
        //转Vo
        List<SupplierVO> collect = supplierList.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
        return collect;
    }
}
